<?php
namespace app\admin\controller;
use app\common\model\Organization;
use think\Validate;
use think\Db;
use think\Session;

class OrganizationController extends BaseController
{
    public function add()
    {
        // 获取团队
        $organization_list = $this->organizationList();
        if(count($organization_list)>0){
            $organization = Db::name('organization')->where(['id'=>['in',$organization_list]])->select();
        }else{
            $organization = [];
        }
        return view('',[
           'organization'=>$organization 
        ]);
    }
    public function lists()
    {
        return view();
    }
    /**
     * 获取团队
     * @param page 页码
     */
    public function getList()
    {
        $page = $this->request->param('page');
        $all = $this->request->param('all');
        $page_size = $this->request->param('page_size');
        if(empty($page_size)){
            $page_size = 10;
        }
        // $where['role_id'] = ['in',[1,2,4,5]];
        // $total = Db::name('organization')->count();
        // $rows = Db::name('organization')->page($page,$page_size)->select();
        if(!empty($all)){//订单明细获取
            $organization_list = $this->organizationList();
            if(count($organization_list)>0){
                $data = Db::name('organization')->where(['id'=>['in',$organization_list]])->select();
            }else{
                $data = Db::name('organization')->select();
            }
            
        }else{//团队管理获取
            $data = Db::name('organization')->paginate($page_size, false, ['query' => request()->param(),'page'=>$page]);
        }
        
        $this->success('获取成功','',$data);
    }

    /**
     * 删除团队
     * @param id 团队id
     */
    public function del()
    {
        $id = $this->request->param('id');
        // 该团队有人，不能删除
        $account_member = Db::name('account_member')->where('organization_id',$id)->find();
        if(!empty($account_member)){
            $this->error('该团队有人，不能删除');
        }
        $data = Db::name('organization')->where('id',$id)->delete();
        if(!empty($data)){
            $this->success('删除成功','',$data);
        }else{
            $this->error('删除失败');
        }
        
    }
     /**
     * 编辑团队
     * @param id 团队id
     * @param name 团队名
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $name = $this->request->param('name');

        $data = Db::name('organization')->where('id',$id)->update(['name'=>$name]);
        if(!empty($data)){
            $this->success('更新成功','',$data);
        }else{
            $this->error('更新失败');
        }
        
    }
    /**
     * 编辑队员
     * @param id 团队id
     * 
     */
    public function member()
    {
        $id = $this->request->param('id');
        // 模板输出
        return view('member',[
            'id' => $id
        ]);
        
    }
        /**
     * 获取成员
     * @param page 页码
     */
    public function getOrganzitionList()
    {
        $id = $this->request->param('id');
        $page = $this->request->param('page');
        $page_size = 10;
        // $where['role_id'] = ['in',[1,2,4,5]];
        // $total = Db::name('organization')->count();
        // $rows = Db::name('organization')->page($page,$page_size)->select();
        $data = Db::name('account_member')->where('organization_id',$id)->paginate($page_size, false, ['query' => request()->param(),'page'=>$page]);
        $this->success('获取成功','',$data);
    }
    /**
     * 删除队员
     * @param id 团队id
     */
    public function delMember()
    {
        $id = $this->request->param('id');
        $data = Db::name('account_member')->where('id',$id)->delete();
        if(!empty($data)){
            $this->success('删除成功','',$data);
        }else{
            $this->error('删除失败');
        }
        
    }
    /**
     * 设置负责人
     * @param id 团队id
     */
    public function editMember()
    {
        $id = $this->request->param('id');
        $is_owner = $this->request->param('is_owner');
        $data = Db::name('account_member')->where('id',$id)->update(['is_owner'=>$is_owner]);
        if(!empty($data)){
            $this->success('操作成功','',$data);
        }else{
            $this->error('操作失败');
        }
        
    }

    /**
     * 编辑团队
     * 
     * @param name 团队名
     */
    public function addName()
    {
        $name = $this->request->param('name');

        $data = Db::name('organization')->insert(['name'=>$name]);
        if(!empty($data)){
            $this->success('添加成功','',$data);
        }else{
            $this->error('添加失败');
        }
        
    }

    /**
     * 加入团队
     * 
     * @param name 用户名
     */
    public function joinOrganization()
    {
        $params = $this->request->param();

        $is_join = Db::name('account_member')->where(['account_id'=>$params['account_id'],'organization_id'=>$params['organization_id']])->find();
        if(!empty($is_join)){
            $this->error('已经添加过了');
        }
        $data = Db::name('account_member')->insert($params);
        if(!empty($data)){
            $this->success('添加成功','',$data);
        }else{
            $this->error('添加失败');
        }
        
    }
    
    public function setStatus()
    {
        $where['id'] = input('post.id');
        $data['status'] = input('post.status');
        $affected = Db::name('account')->where($where)->update($data);
        //return $affected;
        if ($affected) {
            return ['code' => 1, 'msg' => '修改成功'];
        } else {
            return ['code' => 0, 'msg' => '修改失败'];
        }
    }
}